home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / HippoDraw / hippo / hippoio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-28  |  8.1 KB  |  381 lines

  1. /*
  2.  * hippoio.c - input/output routines for hippo package.
  3.  *
  4.  * Copyright (C)  1991  The Board of Trustees of The Leland Stanford
  5.  * Junior University.  All Rights Reserved.
  6.  *
  7.  * $Id: hippoio.c,v 3.12 1992/04/10 22:48:42 rensing Rel $
  8.  *
  9.  *  by Paul Rensing, Feb 28,1991
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "hippo.h"
  16.  
  17. #ifdef VM
  18. #include "h_util.h"
  19. #include "h_xdr.h"
  20. #else
  21. #include "hippoutil.h"
  22. #include "hippoxdr.h"
  23. #endif
  24.  
  25. GLOB_QUAL const char hippoio_c_rcsid[] = 
  26.      "$Id: hippoio.c,v 3.12 1992/04/10 22:48:42 rensing Rel $";
  27.  
  28. static int constructRec( hippo_rec *rec, display dl[], ntuple ntl[] );
  29.  
  30.  
  31. int h_write(const char *filenm, display dlist[], ntuple ntlist[] )
  32. {
  33.      int i;
  34.      FILE *outfile;
  35.      
  36.      /* open the file */
  37.      if ( (outfile = fopen(filenm,"wb")) == NULL)
  38.      {
  39.       h_error("Could not open file");
  40.       return -1;
  41.      }
  42.  
  43.      i = h_writeStream(outfile,dlist,ntlist); 
  44.  
  45.      fclose(outfile);
  46.      
  47.      return i;
  48. }
  49.  
  50. int h_writeStream(FILE *file, display dlist[], ntuple ntlist[] )
  51. {
  52.      XDR xdrs;
  53.      int rc = 0;
  54.      
  55.      /*
  56.       * open the XDR stream.
  57.       */
  58.      xdrstdio_create(&xdrs, file, XDR_ENCODE);
  59.      
  60.      rc = h_writeXDR( &xdrs, dlist, ntlist );
  61.  
  62.      /*
  63.       * destroy the XDR stream
  64.       */
  65.      xdr_destroy(&xdrs);
  66.      
  67.      return rc;
  68. }
  69.      
  70. int h_writeMem(char *buf, int len, display dlist[], ntuple ntlist[] )
  71. {
  72.      XDR xdrs;
  73.      int rc;
  74.      
  75.      /*
  76.       * open the XDR stream.
  77.       */
  78.      xdrmem_create(&xdrs, buf, len, XDR_ENCODE);
  79.  
  80.      rc = h_writeXDR(&xdrs, dlist, ntlist );
  81.  
  82.      /*
  83.       * destroy the XDR stream
  84.       */
  85.      xdr_destroy(&xdrs);
  86.      
  87.      return rc;
  88. }
  89.  
  90. int h_writeXDR(XDR * xdrs, display dlist[], ntuple ntlist[] )
  91. {
  92.      hippo_rec record;
  93.      int rc = 0;
  94.      int i;
  95.      /*
  96.       * construct the hippo_rec to pass to XDR
  97.       */
  98.      if (constructRec( &record, dlist, ntlist ) != 0)
  99.      {
  100.       h_error("Error constructing hippo record.");
  101.       return -1;
  102.      }
  103.           
  104.      if (record.num_disp == 0 && record.num_nt == 0)
  105.       return 0;
  106.      
  107.      /* 
  108.       * !!!!! displays have been modified, so don't crash now!!
  109.       */
  110.  
  111.      /*
  112.       * write the record.
  113.       */
  114.      if (!xdr_hippo_rec(xdrs, &record ))
  115.      {
  116.       rc = -1;
  117.       h_error("Error writing XDR file");
  118.      }
  119.      
  120.  
  121.      /*
  122.       * undo changes to displays
  123.       */
  124.      for (i=0; i<record.num_disp; i++)
  125.      {
  126.       if ((int)record.disp_list[i]->ntuple == -1)
  127.            record.disp_list[i]->ntuple = NULL;
  128.       else if (! record.disp_list[i]->flags.ntByReference)
  129.       {
  130.            record.disp_list[i]->ntuple =
  131.             record.nt_list[ (int) record.disp_list[i]->ntuple ];
  132.       }
  133.      }
  134.      
  135.      /*
  136.       * free the lists in the record
  137.       */
  138.      if (record.nt_list != NULL) free(record.nt_list);
  139.      if (record.disp_list != NULL) free(record.disp_list);
  140.  
  141.      return rc;
  142. }
  143.  
  144.  
  145. static int constructRec(hippo_rec *rec, display dlist[],
  146.             ntuple ntlist[] )
  147. {
  148.      int i, j;
  149.      int num_nt;
  150.      int inlist;
  151.      
  152.      rec->num_nt = 0;
  153.      if (ntlist != NULL)
  154.       while (ntlist[rec->num_nt] != NULL) rec->num_nt++;
  155.  
  156.      rec->num_disp = 0;
  157.      if (dlist != NULL)
  158.       while (dlist[rec->num_disp] != NULL) rec->num_disp++;
  159.      
  160.      /*
  161.       * check that there is something to write.
  162.       */
  163.      if (rec->num_disp == 0 && rec->num_nt == 0)
  164.       return 0;
  165.      
  166.      rec->disp_list = NULL;
  167.      if (rec->num_disp > 0 && (rec->disp_list = (display *) 
  168.                    malloc( rec->num_disp * sizeof(display))
  169.                    )==NULL)
  170.      {
  171.       h_error("Could not allocate memory for temporary display list");
  172.       return -1;
  173.      }
  174.  
  175.      if ((rec->nt_list = (ntuple *) 
  176.       malloc((rec->num_disp+rec->num_nt) * sizeof(ntuple) ))==NULL)
  177.      {
  178.       h_error("Could not allocate memory for temporary ntuple array");
  179.       return -1;
  180.      }
  181.    
  182.      /* 
  183.       * copy given list of n-tuples. Check for duplicates.
  184.       */
  185.      num_nt = 0;
  186.      for (i=0; i<rec->num_nt; i++)
  187.      {
  188.       inlist = 0;
  189.       for (j=0; j<num_nt; j++)
  190.            inlist |= (ntlist[i] == rec->nt_list[j]);
  191.       if (!inlist)
  192.            rec->nt_list[num_nt++] = ntlist[i];
  193.      }
  194.      rec->num_nt = num_nt;
  195.      
  196.      /*
  197.       * now, add any n-tuples not in list which are needed by displays.
  198.       * compile list of disp->ntuple to ntuple index.
  199.       * disp->ntuple == NULL --> -1.
  200.       */
  201.      for (i=0; i<rec->num_disp; i++)
  202.      {
  203.       rec->disp_list[i] = dlist[i];
  204.       if (! rec->disp_list[i]->flags.ntByReference)
  205.       {
  206.            if (rec->disp_list[i]->ntuple == NULL || 
  207.            rec->disp_list[i]->bins.flags.fixed) 
  208.            {
  209.             rec->disp_list[i]->ntuple = (ntuple) -1;
  210.            }
  211.            else
  212.            {
  213.             for (j=0; j<rec->num_nt; j++) 
  214.             {
  215.              if (rec->disp_list[i]->ntuple == rec->nt_list[j])
  216.              {
  217.                   rec->disp_list[i]->ntuple = (ntuple) j;
  218.                   break;
  219.              }
  220.             }
  221.             if (j >= rec->num_nt)
  222.             {
  223.              rec->nt_list[rec->num_nt] = rec->disp_list[i]->ntuple;
  224.              rec->disp_list[i]->ntuple = (ntuple) rec->num_nt++;
  225.             }
  226.            }
  227.       }
  228.      }
  229.  
  230.      rec->magic = MAGIC;
  231.      rec->s_version = STRUCT_VERSION;
  232.      
  233.  
  234.      return 0;
  235. }
  236.  
  237.  
  238. int h_read(const char *filenm, display **dlist, ntuple **ntlist )
  239. {
  240.      int i;
  241.      FILE *infile;
  242.      
  243.      /* open the file */
  244.      if ( (infile = fopen(filenm,"rb")) == NULL)
  245.      {
  246.       h_error("Could not open input file");
  247.       return -1;
  248.      }
  249.  
  250.      i = h_readStream(infile,dlist,ntlist);
  251.  
  252.      fclose(infile);
  253.      
  254.      return i;
  255. }
  256.  
  257. int h_readStream(FILE *file, display **dlist, ntuple **ntlist )
  258. {
  259.      XDR xdrs;
  260.      int rc;
  261.      
  262.      /*
  263.       * open the XDR stream.
  264.       */
  265.      xdrstdio_create(&xdrs, file, XDR_DECODE);
  266.  
  267.      rc = h_readXDR(&xdrs, dlist, ntlist );
  268.      /*
  269.       * destroy the XDR stream
  270.       */
  271.      xdr_destroy(&xdrs);
  272.      
  273.      return rc;
  274. }
  275.  
  276. int h_readMem(char *buf, int len, display **dlist, ntuple **ntlist )
  277. {
  278.      XDR xdrs;
  279.      int rc;
  280.      
  281.      /*
  282.       * open the XDR stream.
  283.       */
  284.      xdrmem_create(&xdrs, buf, len, XDR_DECODE);
  285.  
  286.      rc = h_readXDR(&xdrs, dlist, ntlist );
  287.      /*
  288.       * destroy the XDR stream
  289.       */
  290.      xdr_destroy(&xdrs);
  291.      
  292.      return rc;
  293. }
  294.  
  295.  
  296. int h_readXDR(XDR *xdrs, display **dlist, ntuple **ntlist )
  297. {
  298.      hippo_rec record;
  299.      int i,j;
  300.      func_id p;
  301.       
  302.      record.magic = MAGIC;
  303.      record.s_version = STRUCT_VERSION;
  304.      record.num_nt = record.num_disp = 0;
  305.      record.nt_list = NULL;
  306.      record.disp_list = NULL;
  307.      
  308.      /*
  309.       * read the record.
  310.       */
  311.      if (!xdr_hippo_rec(xdrs, &record ))
  312.      {
  313.       h_error("Error reading XDR file");
  314.       return -1;
  315.      }
  316.      
  317.      /*
  318.       * allocate output records.
  319.       */
  320.      if ((*ntlist=(ntuple *) 
  321.       malloc( (record.num_nt+1) * sizeof(ntuple)))==NULL)
  322.      {
  323.       h_error("Could not allocate memory for ntuple array");
  324.       return -1;
  325.      }
  326.  
  327.      if ((*dlist=(display *) 
  328.       malloc( (record.num_disp+1) * sizeof(display)))==NULL)
  329.      {
  330.       h_error("Could not allocate memory for display array");
  331.       return -1;
  332.      }
  333.      
  334.      
  335.      /*
  336.       * copy lists and undo changes to displays.
  337.       */
  338.      for (i=0; i<record.num_disp; i++)
  339.      {
  340.       (*dlist)[i] = record.disp_list[i];
  341.       if ( !(*dlist)[i]->flags.ntByReference )
  342.       {
  343.            j = (int) (*dlist)[i]->ntuple;
  344.            if ( j < 0 || j >= record.num_nt ) {
  345.             (*dlist)[i]->ntuple = NULL;
  346.            } else {
  347.             (*dlist)[i]->ntuple = record.nt_list[j];
  348.            }
  349.       }
  350.       
  351.       /*
  352.        * resolve function references.
  353.        */
  354.       for (p=(*dlist)[i]->nt_cut; p!=NULL; p=p->next )
  355.            if( (p->funcPtr = h_fNameSrch(p->name)) == NULL)
  356.             h_error("Cut function name is not in registry");
  357.  
  358.       for (p=(*dlist)[i]->bin_func; p!=NULL; p=p->next )
  359.            if( (p->funcPtr = h_fNameSrch(p->name)) == NULL)
  360.             h_error("Cut function name is not in registry");
  361.  
  362.       for (p=(*dlist)[i]->plot_func; p!=NULL; p=p->next )
  363.            if( (p->funcPtr = h_fNameSrch(p->name)) == NULL)
  364.             h_error("Cut function name is not in registry");
  365.      }
  366.      (*dlist)[record.num_disp] = NULL;
  367.      for (i=0; i<record.num_nt; i++)
  368.      {
  369.       (*ntlist)[i] = record.nt_list[i];
  370.      }
  371.      (*ntlist)[record.num_nt] = NULL;
  372.      
  373.      /*
  374.       * free the lists in the record
  375.       */
  376.      if (record.nt_list != NULL) free(record.nt_list);
  377.      if (record.disp_list != NULL) free(record.disp_list);
  378.      
  379.      return 0;
  380. }
  381.